package com.moon.proxy;

import com.moon.cglib.TicketsStation;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

@Slf4j
public class ProxyFactory {

    private TicketsStation ticketsStation = new TicketsStation();


    public SellTickets generate() {
        Object newProxyInstance = Proxy.newProxyInstance(
                ticketsStation.getClass().getClassLoader(),
                ticketsStation.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//                        System.out.println(proxy);
                        log.info("{}", "增收一些服务费");
                        return method.invoke(ticketsStation, args);
                    }
                }
        );
        return  (SellTickets) newProxyInstance;
    }
}
